package com.qms.repast.utils;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentCallbacks;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;

/**
 * @author 泅渡者
 * @date 2018/9/6 0006
 * @describe:
 */
public class Density {
	private static float appDensity;
	private static float appScaledDensity;
	private static DisplayMetrics appDisplayMetrics;

	private static float WIDTH;

	public static void setDensity(@NonNull final Application application, float width) {
		appDisplayMetrics = application.getResources().getDisplayMetrics();
		WIDTH = width;
		registerActivityLifecycleCallbacks(application);

		if (appDensity == 0) {
			appDensity = appDisplayMetrics.density;
			appScaledDensity = appDisplayMetrics.scaledDensity;
			application.registerComponentCallbacks(new ComponentCallbacks() {
				@Override
				public void onConfigurationChanged(Configuration newConfig) {
					if (newConfig != null && newConfig.fontScale > 0) {
						appScaledDensity = application.getResources().getDisplayMetrics().scaledDensity;
					}
				}

				@Override
				public void onLowMemory() {
				}
			});
		}
	}


	private static void setDefault(Activity activity) {
		setAppOrientation(activity);
	}

	private static void setAppOrientation(@Nullable Activity activity) {

		float targetDensity = 0;
		try {
			targetDensity = appDisplayMetrics.widthPixels / WIDTH;
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		float targetScaledDensity = targetDensity * (appScaledDensity / appDensity);
		int targetDensityDpi = (int) (160 * targetDensity);
		DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics();
		activityDisplayMetrics.density = targetDensity;
		activityDisplayMetrics.scaledDensity = targetScaledDensity;
		activityDisplayMetrics.densityDpi = targetDensityDpi;
	}


	private static void registerActivityLifecycleCallbacks(Application application) {
		application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
			@Override
			public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
				setDefault(activity);
			}
			@Override
			public void onActivityStarted(Activity activity) {
			}
			@Override
			public void onActivityResumed(Activity activity) {
			}
			@Override
			public void onActivityPaused(Activity activity) {
			}
			@Override
			public void onActivityStopped(Activity activity) {
			}
			@Override
			public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
			}
			@Override
			public void onActivityDestroyed(Activity activity) {

			}
		});
	}
}
